// Logging.cpp: Implementierung der Klasse Logging.
//
//////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include "lncppapi.h"
#include "Logging.h"

Logging* Logging::instance = 0;

//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////

Logging::Logging()
{
	initialised = false;
	level=LOG_INFO;
	program=NULL;
}

Logging::~Logging()
{

}

Logging* Logging::GetInstance() 
{
	if (instance==0) {
		instance = new Logging();
	}

	return instance;
}
void Logging::RemoveInstance()
{
	if(instance!=0) {
		delete instance;
		instance=0;
	}
}

void Logging::write(const char* string)
{
	char message_string[256];
	if (program!=NULL) {
		strcpy(message_string,program);
		strcat(message_string,": ");
		strcat(message_string,string);
	} else {
		strcpy(message_string,string);
	}

	if (initialised==false)
		write_console(message_string);
	else
		write_addin(message_string);
}

void Logging::write_console(const char* string)
{
	printf("%s\n",string);
}

void Logging::write_addin(const char* string)
{
	addin.AppendLogMessage(string);
}

void Logging::initialise(LNNotesSession& session, const char* aProgram)
{
	session.GetServerAddin(program,"Initialising", &addin);
	program=(char*) aProgram;
}

const char* Logging::GetErrorMessage(LNSTATUS lnerror) {
	LNString errormessage;
    LNGetErrorMessage(lnerror, ErrorBuf, LNERROR_MESSAGE_LENGTH);
	return ErrorBuf;
}

void Logging::write(const char* string, LNSTATUS lnerror) {
	LNString errormsg = GetErrorMessage(lnerror);
	errormsg.Append(string);
	write(errormsg);
}